package cn.yunyichina.provider.message.alipay.factory;

import java.util.HashMap;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;

/**
 * API调用客户端工厂
 *
 */
public class AlipayAPIClientFactory {

	/** API调用客户端 */
	private static AlipayClient alipayClient;

	/** 支付宝网关 */
	public static final String ALIPAY_GATEWAY = "https://openapi.alipay.com/gateway.do";
	
	/**
	 * 获得API调用客户端
	 * 
	 * @return
	 */

	public static AlipayClient getAlipayClient(String appId, String private_key, String type, String charset) {

		if (clients.containsKey(appId)) {
			return clients.get(appId);
		}
		alipayClient = new DefaultAlipayClient(ALIPAY_GATEWAY, appId, private_key, type, charset);
		clients.put(appId, alipayClient);
		return alipayClient;
	}

	/**
	 * 获得API RSA2调用客户端
	 *
	 * @return
	 */

	public static AlipayClient getAlipayClientRSA2(String appId, String privateKey, String type, String charset , String publicKey) {

		if (clients.containsKey(appId)) {
			return clients.get(appId);
		}
		alipayClient = new DefaultAlipayClient(ALIPAY_GATEWAY, appId, privateKey, type, charset , publicKey , "RSA2");
		clients.put(appId, alipayClient);
		return alipayClient;
	}

	private static HashMap<String, AlipayClient> clients = new HashMap<String, AlipayClient>();

}
